import svelte from 'rollup-plugin-svelte';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import terser from '@rollup/plugin-terser';
import css from 'rollup-plugin-css-only';
import serve from 'rollup-plugin-serve';
import sveltePreprocess from 'svelte-preprocess';
import image from '@rollup/plugin-image';
import copy from 'rollup-plugin-copy';

const production = !process.env.ROLLUP_WATCH;

// function serve() {
// 	let server;

// 	function toExit() {
// 		if (server) server.kill(0);
// 	}

// 	return {
// 		writeBundle() {
// 			if (server) return;
// 			server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
// 				stdio: ['ignore', 'inherit', 'inherit'],
// 				shell: true
// 			});

// 			process.on('SIGTERM', toExit);
// 			process.on('exit', toExit);
// 		}
// 	};
// }

const lib = process.env.LIB || false;
export default {
	isLib: lib,
	input: lib ? "src/comps/"+lib+".svelte" : 'src/main.js',
	output: lib ? {
		sourcemap: true,
		format: 'iife',
		name: lib,
		file: 'public/build/'+lib+'.js',
	} : {
		sourcemap: true,
		format: 'iife',
		name: 'app',
		file: 'public/build/bundle.js',
	},
	plugins: [
		copy({
      targets: [
        { src: 'src/asset', dest: 'public' },
      ],
			copyOnce: production,
      verbose: true
    }),
		image({
			// exclude: 'src/asset'
		}),
		svelte({
			compilerOptions: {
				customElement: !!lib,
				// enable run-time checks when not in production
				dev: !production
			},
			onwarn: (warning, handler) => {
				// e.g. don't warn on <marquee> elements, cos they're cool
				if (warning.code === 'PLUGIN_WARNING') return;
				else if (warning.code === 'a11y-click-events-have-key-events') return;
				else if (warning.code === 'a11y-distracting-elements') return;
				else if (warning.code === 'a11y-role-has-required-aria-props') return;
				else if (warning.code === 'a11y-no-noninteractive-tabindex') return;
				else if (warning.code === 'a11y-mouse-events-have-key-events') return;
				else if (warning.code === 'a11y-missing-content') return;
				else if (warning.code === 'a11y-invalid-attribute') return;
				else if (warning.code === 'css-unused-selector') return;
				else if (warning.code === 'unused-export-let') return;
				else if (warning.code === 'missing-custom-element-compile-options') return;
				// console.log('---',warning)
				// let Rollup handle all other warnings normally
				handler(warning);
			},
			preprocess: sveltePreprocess()
		}),
		// we'll extract any component CSS out into
		// a separate file - better for performance
		css({ output: 'bundle.css' }),

		// If you have external dependencies installed from
		// npm, you'll most likely need these plugins. In
		// some cases you'll need additional configuration -
		// consult the documentation for details:
		// https://github.com/rollup/plugins/tree/master/packages/commonjs
		resolve({
			browser: true,
			dedupe: ['svelte']
		}),
		commonjs(),

		// In dev mode, call `npm run start` once
		// the bundle has been generated
		// !production && serve(),
		!production && serve({
			contentBase: 'public',
			port: 3000,
		}),

		// Watch the `public` directory and refresh the
		// browser on changes when not in production
		!production && livereload('public'),

		// If we're building for production (npm run build
		// instead of npm run dev), minify
		production && terser()
	],
	watch: {
		clearScreen: false
	}
};
